package com.zhiyou100.video.dao.impl;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.zhiyou100.video.dao.CourseDao;
import com.zhiyou100.video.model.Course;
import com.zhiyou100.video.model.Subject;
import com.zhiyou100.video.model.Video;
@SuppressWarnings("all")
public class CourseDaoImpl extends HibernateDaoSupport implements CourseDao {

	@Override
	public Integer selectCourseConut() {
		return getHibernateTemplate().find("from Course").size();
	}

	
	@Override
	public List<Course> selectCourse(Integer currentpage) {
		DetachedCriteria dc = DetachedCriteria.forClass(Course.class);
		return (List<Course>) getHibernateTemplate().findByCriteria(dc, (currentpage-1)*5, 5);
	}


	@Override
	public List<Subject> selectSubject() {
		return (List<Subject>) getHibernateTemplate().find("from Subject");
	}


	@Override
	public void addCourse(Course course) {
		getHibernateTemplate().saveOrUpdate(course);
	}


	@Override
	public Course selectCourseId(Integer id) {
		return getHibernateTemplate().get(Course.class, id);
	}


	@Override
	public void deletecourse(Integer did) {
		getHibernateTemplate().delete(getHibernateTemplate().get(Course.class, did));
	}

	@Override
	public List<Course> selectIndexVideo(DetachedCriteria dc) {
		return (List<Course>) getHibernateTemplate().findByCriteria(dc);
	}


	@Override
	public List<Subject> selectvideoindex(DetachedCriteria dc) {
		return (List<Subject>) getHibernateTemplate().findByCriteria(dc);
	}


	@Override
	public void addViceoState(Integer videoId) {
				Video video = getHibernateTemplate().get(Video.class, videoId);
				video.setVideoPlayTimes(video.getVideoPlayTimes()+1);
	}
	
	
	
	@Override
	public List<Object[]> selectVideo() {
		List<Object[]> list = (List<Object[]>) getHibernateTemplate().find("select cast(avg(videoPlayTimes) as int),cid.courseName from Video group by cid.courseName");
		
		return list;
	}
	
}
